package com.HRs.UI;


import com.HRs.Controller.dtAController;
import com.HRs.domain.Patient;
import com.HRs.domain.Visit;
import com.HRs.utils.Account;
import com.HRs.utils.JDBCUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class Hall_dtA {
    JFrame frame = new JFrame("科室管理员后台");
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;
    final int H = 770, W = 720;

    DefaultTableModel tableModel_v;
    JTable table_v;
    String[][] table_Visit = new String[100][100];
    String[] columnId_v = {"医生编号","就诊时间","就诊人数上限","科室"};
    String[] c_v = {"v_Id", "v_Time", "v_MaxNum", "v_Department"};

    DefaultTableModel tableModel_p;
    JTable table_p;
    String[][] table_Patient = new String[100][100];
    String[] columnId_p = {"病人编号","姓名","性别", "电话号码","身份证号", "住址"};

    int selected_row;
    int selected_col;

    //    Date d = new Date();
//    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String Date = "";

    public Hall_dtA(){
        frame.setLayout(null);

        JPanel p1 = new JPanel();
        p1.setBounds(0, 0, 230 ,80);
        p1.setBackground(Color.DARK_GRAY);
        JLabel l1 = new JLabel("后台管理");
        l1.setFont(new Font("隶书", Font.PLAIN, 35));
        l1.setForeground(Color.WHITE);
        p1.add(BorderLayout.CENTER, l1);
        frame.add(p1);

        JPanel p2 = new JPanel();
        p2.setBounds(230, 0, 770, 80);
        p2.setBackground(Color.GRAY);
        JLabel l2 = new JLabel("科室管理员后台");
        l2.setFont(new Font("隶书", Font.PLAIN, 35));
        l2.setForeground(Color.WHITE);
        p2.add(l2);
        frame.add(p2);

        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(8, 1));
        p3.setBackground(Color.GRAY);
        p3.setBounds(0, 80, 230, 800);

        JButton b_ChkDoc = new JButton("医生就诊信息");
        b_ChkDoc.setFont(new Font("隶书", Font.PLAIN, 25));
        b_ChkDoc.setBounds(0, 100, 200, 50);
        b_ChkDoc.setContentAreaFilled(false);
        p3.add(b_ChkDoc);

        JButton b_SearchP = new JButton("科室病人信息");
        b_SearchP.setFont(new Font("隶书", Font.PLAIN, 25));
        b_SearchP.setBounds(0, 50, 200, 50);
        b_SearchP.setContentAreaFilled(false);
        p3.add(b_SearchP);


        JButton b_Return = new JButton("返回登录界面");
        b_Return.setFont(new Font("隶书", Font.PLAIN, 25));
        b_Return.setBounds(0, 150, 200, 50);
        b_Return.setContentAreaFilled(false);
        b_Return.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Login();
                frame.dispose();
            }
        });
        p3.add(b_Return);
        frame.add(p3);

        patient();
        visit();

        b_ChkDoc.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel2.setVisible(false);
                panel3.setVisible(true);
            }
        });

        b_SearchP.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel3.setVisible(false);
                panel2.setVisible(true);
            }
        });

        frame.setSize(1000, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void patient(){
        panel2 = new JPanel();
        panel2.setBounds(230, 80, H, W);
        panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));

        JPanel t2 = new JPanel();
//        get_p(table_Patient);
        tableModel_p = new DefaultTableModel(table_Patient, columnId_p);
        table_p = new JTable(tableModel_p);
        table_p.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        table_p.getTableHeader().setFont(new Font("隶书",Font.PLAIN,16));
        JScrollPane sc = new JScrollPane(table_p);
        t2.add(sc);
        panel2.add(t2);

        JPanel t1 = new JPanel();
        JLabel timelbl = new JLabel("日期:");
        timelbl.setFont(new Font("隶书", Font.PLAIN, 25));
        t1.add(timelbl);
        JTextField txtTime = new JTextField("格式为yyyy-MM-dd",20);
        txtTime.setFont(new Font("隶书", Font.ITALIC, 20));
        t1.add(txtTime);
        JButton b_search = new JButton("查找");
        b_search.setFont(new Font("隶书", Font.ITALIC, 20));
        b_search.setContentAreaFilled(false);
        t1.add(b_search);

        JLabel cntlbl = new JLabel("预约人数:");
        cntlbl.setFont(new Font("隶书", Font.PLAIN, 25));
        t1.add(cntlbl);
        JButton b_Count = new JButton("统计");
        b_Count.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Count.setContentAreaFilled(false);
        t1.add(b_Count);
        b_search.addActionListener(new ActionListener() {//查找
            @Override
            public void actionPerformed(ActionEvent e) {
                Date = txtTime.getText();
//                get_p(table_Patient);
//                get_p();
                flush_p();
            }
        });
        b_Count.addActionListener(new ActionListener() {//统计
            @Override
            public void actionPerformed(ActionEvent e) {
                dtAController dC = new dtAController();
                String dt = txtTime.getText();
                SetZero();
                dC.flush_(dt);
                JOptionPane.showMessageDialog(b_Count, "本科室预约人数为:" + dC.count_OdNum(Account.dta_id) , "统计结果", JOptionPane.PLAIN_MESSAGE);

            }
        });
        panel2.add(t1);

        panel2.setVisible(false);
        frame.add(panel2);
    }

    public void visit(){
        panel3 = new JPanel();
        panel3.setBounds(230, 80, H, W);
        panel3.setLayout(new BoxLayout(panel3,BoxLayout.Y_AXIS));

        JPanel t2 = new JPanel();
        get_v(table_Visit);
        tableModel_v = new DefaultTableModel(table_Visit, columnId_v);
        table_v = new JTable(tableModel_v);
        table_v.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        table_v.getTableHeader().setFont(new Font("隶书",Font.PLAIN,18));
        JScrollPane sc = new JScrollPane(table_v);
        t2.add(sc);
        panel3.add(t2);

        JPanel t3 = new JPanel();
        JLabel lblId = new JLabel("医生编号:");
        lblId.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtId = new JTextField(10);
        t3.add(lblId);
        t3.add(txtId);
        JLabel lblTime = new JLabel("就诊时间:");
        lblTime.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtTime = new JTextField(10);
        t3.add(lblTime);
        t3.add(txtTime);
        JLabel lblMaxNum = new JLabel("就诊人数上限:");
        lblMaxNum.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtMaxNum = new JTextField(10);
        t3.add(lblMaxNum);
        t3.add(txtMaxNum);
        JLabel lblDP = new JLabel("科室:");
        lblDP.setFont(new Font("隶书", Font.PLAIN, 15));
        JTextField txtDp = new JTextField(10);
        t3.add(lblDP);
        t3.add(txtDp);
        panel3.add(t3);

        JPanel t4 = new JPanel();
        JButton b_delete = new JButton("删除");
        JButton b_update = new JButton("修改");
        JButton b_insert = new JButton("添加");
        JButton b_Flush = new JButton("刷新");
        b_delete.setFont(new Font("隶书", Font.ITALIC, 20));
        b_insert.setFont(new Font("隶书", Font.ITALIC, 20));
        b_update.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Flush.setFont(new Font("隶书", Font.ITALIC, 20));
        b_Flush.setContentAreaFilled(false);
        b_delete.setContentAreaFilled(false);
        b_insert.setContentAreaFilled(false);
        b_update.setContentAreaFilled(false);

        b_Flush.addActionListener(new ActionListener() {//刷新
            @Override
            public void actionPerformed(ActionEvent e) {
                flush_v();
            }
        });

        b_delete.addActionListener(new ActionListener() {//删除
            @Override
            public void actionPerformed(ActionEvent e) {
                dtAController dC = new dtAController();
                String ID, info;
                info = (String) table_v.getValueAt(selected_row, selected_col);
                ID = c_v[selected_col];
                dC.delete_v(ID, info);
                flush_v();
            }
        });

        b_insert.addActionListener(new ActionListener() {//添加
            @Override
            public void actionPerformed(ActionEvent e) {
                dtAController dC = new dtAController();
                String id, time, Dp;
                String MaxNum;
                id = txtId.getText();
                time = txtTime.getText();
                MaxNum = (String)txtMaxNum.getText();
                Dp = txtDp.getText();
                Visit v = new Visit(id, time, MaxNum, Dp);
                dC.insert_v(v, Account.dta_id);
                flush_v();
            }
        });

        b_update.addActionListener(new ActionListener() {//修改
            @Override
            public void actionPerformed(ActionEvent e) {
                dtAController dC = new dtAController();
                String ID, info;
                int col;
                if(selected_col == 0) col = selected_col + 1;
                else if(selected_col == 3) col = selected_col - 1;
                else col = selected_col - 1;
                info = (String) table_v.getValueAt(selected_row, col);
                ID = c_v[col];
//                System.out.println("ID : " + ID + " info : " + info);
                dC.delete_v(ID, info);
                String id, time, Dp;
                String MaxNum;
                id = (String) table_v.getValueAt(selected_row, 0);
                time = (String) table_v.getValueAt(selected_row, 1);
                Dp = (String) table_v.getValueAt(selected_row, 3);
                MaxNum = (String) table_v.getValueAt(selected_row, 2);
                Visit v = new Visit(id, time, MaxNum, Dp);
                String[] information = {id, time, MaxNum, Dp};
                tableModel_v.insertRow(selected_row, information);
                dC.insert_v(v, Account.dta_id);
                flush_v();
            }
        });

        table_v.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selected_row = table_v.getSelectedRow();
                selected_col = table_v.getSelectedColumn();
            }
        });

        t4.add(b_insert);
        t4.add(b_delete);
        t4.add(b_update);
        t4.add(b_Flush);
        panel3.add(t4);

        frame.add(panel3);
    }

    public void flush_v(){
        String[][] table_F = new String[100][100];
        get_v(table_F);

        DefaultTableModel dtm = (DefaultTableModel) table_v.getModel();
        dtm.setDataVector(table_F, columnId_v);
        table_v.updateUI();
    }

    public void get_v(String[][] table_Visit){
        ArrayList<Visit> Vit = new ArrayList<Visit>();
        dtAController dC = new dtAController();

        Vit = dC.get_V(Account.dta_id);

        for(int i = 0; i < Vit.size(); i ++ ){
            table_Visit[i][0] = Vit.get(i).getV_Id();
            table_Visit[i][1] = Vit.get(i).getV_Time();
            table_Visit[i][2] = Vit.get(i).getV_MaxNum();
            table_Visit[i][3] = Vit.get(i).getV_Department();
        }
    }

    public void flush_p(){
        String[][] table_F = new String[100][100];
        get_p(table_F);

        DefaultTableModel dtm = (DefaultTableModel) table_p.getModel();
        dtm.setDataVector(table_F, columnId_p);
        table_p.updateUI();
    }

    public void get_p(String[][] table_Patient){
        ArrayList<Patient> ap = new ArrayList<>();
        dtAController dC = new dtAController();

        ap = dC.get_P(Date);

        for(int i = 0; i < ap.size(); i ++ ){
            table_Patient[i][0] = ap.get(i).getP_Pid();
            table_Patient[i][1] = ap.get(i).getP_Name();
            table_Patient[i][2] = ap.get(i).getP_Password();
            table_Patient[i][3] = ap.get(i).getP_Sex();
            table_Patient[i][4] = ap.get(i).getP_PhoneNumber();
            table_Patient[i][5] = ap.get(i).getP_Address();
            table_Patient[i][6] = ap.get(i).getP_ID();
        }
    }

    public void SetZero(){
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "call flush_SetZero()";
            st.execute(sql);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }
    }

    public static void main(String[] args) {
        new Hall_dtA();
    }
}

